<template>
	<view>
		<!-- 跳转到回调地址 -->
		<!-- <web-view :webview-styles="webviewStyles" :src="url"></web-view> -->
	</view>
</template>

<script>
// import { wxpayUnifiedorder } from '@/api/payment'
import { buyVip, pointsRecharge, accountRecharge, mallBuy } from '@/api/payment'
import { mapState } from 'vuex'
export default {
	name: 'WxPayment',
	data() {
		return {
			// webview进度条样式配置
			webviewStyles: {
				progress: {
					color: '#FF3333'
				}
			},
			// 支付成功/失败回调url
			success_url: '',
			cancel_url: '',
			requestParams: {},
			payParams: {},
			from: ''
		}
	},
	onLoad(options) {
		// console.log(options, 'options')
		const { from, success_url, cancel_url, token } = options
		if (token) {
			uni.setStorageSync('token', token)
		}
		this.from = from
		this.success_url = success_url
		this.cancel_url = cancel_url
		this.requestParams = { ...JSON.parse(decodeURIComponent(options.requestParams)), openid: this.openid, platform: 'MP-WEIXIN' }
		// console.log(this.requestParams, 'requestParams')
		switch (this.from) {
			case 'vip':
				buyVip(this.requestParams).then(res => {
			    this.wxpay(JSON.parse(res.data.pay_button))
				})
				break
			case 'Integral':
				pointsRecharge(this.requestParams).then(res => {
			    this.wxpay(JSON.parse(res.data.pay_button))
				})
				break
			case 'Recharge':
				accountRecharge(this.requestParams).then(res => {
			    this.wxpay(JSON.parse(res.data.pay_button))
				})
				break
			case 'mall':
				mallBuy(this.requestParams).then(res => {
			    this.wxpay(JSON.parse(res.data))
				})
				break
			default:
				break
		}
	},
	computed: {
		...mapState({
			openid: state => state.login.loginInfo.openid
		})
	},
	methods: {
		wxpay(payParams) {
			// console.log(payParams, 'payParams')
			const { timeStamp, nonceStr, signType, paySign } = payParams
			const that = this
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: timeStamp + '',
				nonceStr: nonceStr,
				package: payParams.package,
				signType: signType,
				paySign: paySign,
				success: res => {
					// console.log(res)
					// console.log('支付成功')
					if (that.success_url) {
						let successUrl = this.from == 'mall' ? decodeURIComponent(that.success_url) : that.success_url
						uni.redirectTo({
						    url: successUrl
					    })
					} else {
						uni.navigateBack()
					}
				},
				fail: res => {
					// console.log(res)
					// console.log('支付失败')
					if (that.cancel_url) {
						uni.redirectTo({
						    url: that.cancel_url
					    })
					} else {
						uni.navigateBack()
					}
				}
			})
		}
	}
}
</script>
